"""
Case Type   : 资源池化系统视图
Case Name   : 默认配置下，执行switchover操作，查看系统视图dbe_perf.track_memory_context
Create At   : 2025/2
Owner       : haomeng
Description :
    1、主节点查看系统表dbe_perf.track_memory_context中DMSDrcContext字段是否存在
    2、备节点查看系统表dbe_perf.track_memory_context中DMSDrcContext字段是否存在
    3、执行switchover操作切换主备节点
    4、新主节点查看系统表dbe_perf.track_memory_context中DMSDrcContext字段是否存在
    5、新备节点查看系统表dbe_perf.track_memory_context中DMSDrcContext字段是否存在
    6、恢复初始环境
Expect      :
    1、无相关字段及信息
    2、无相关字段及信息
    3、切换成功
    4、无相关字段及信息
    5、无相关字段及信息
    6、环境恢复成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class ShareStorageSystemView(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()

        self.sys_view1 = 'dbe_perf.track_memory_context'
        self.sys_view2 = 'dbe_perf.track_memory_context_detail()'

    def test_ss_system_view(self):
        text = '-----step1:主节点查看系统表dbe_perf.track_memory_context中DMSMemContext字段是否存在  ' \
               'expect:无相关字段及信息-----'
        self.log.info(text)

        self.log.info('---设置需要统计的内存申请详细信息的内存上下文---')
        sql1 = f"select * from {self.sys_view1}('DMSMemContext');"
        self.log.info(sql1)
        res1 = self.pri_sh.execut_db_sql(sql1)
        self.log.info(res1)
        self.assertIn('t', res1.splitlines()[-2].strip(), '内存上下文设置失败')

        sql2 = f"select * from {self.sys_view2};"
        self.log.info(sql2)
        res2 = self.pri_sh.execut_db_sql(sql2, dbname='postgres')
        self.log.info(res2)
        self.assertIn('(0 rows)', res2, '存在异常信息')

        text = '-----step2:备节点查看系统表dbe_perf.track_memory_context中DMSMemContext字段是否存在  ' \
               'expect:无相关字段及信息-----'
        self.log.info(text)

        res3 = self.sta_sh.execut_db_sql(sql1)
        self.log.info(res3)
        self.assertIn('t', res3.splitlines()[-2].strip(), '内存上下文设置失败')

        res4 = self.sta_sh.execut_db_sql(sql2)
        self.log.info(res4)
        self.assertIn('(0 rows)', res4, '存在异常信息')

        text = '-----step3:执行switchover操作切换主备节点  expect:切换成功-----'
        self.log.info(text)

        switchover = self.pri_sh.exec_cm_ctl('switchover', '-n 2',
                                                   f'-D {macro.DB_INSTANCE_PATH}')
        self.log.info(switchover)
        self.assertIn(self.constant.cm_switchover_success_msg, switchover, '执行失败')

        text = '-----step4:新主节点查看系统表dbe_perf.track_memory_context中DMSMemContext字段是否存在  ' \
               'expect:无相关字段及信息-----'
        self.log.info(text)

        res5 = self.sta_sh.execut_db_sql(sql1)
        self.log.info(res4)
        self.assertIn('t', res5.splitlines()[-2].strip(), '内存上下文设置失败')

        res6 = self.sta_sh.execut_db_sql(sql2)
        self.log.info(res6)
        self.assertIn('(0 rows)', res6, '存在异常信息')

        text = '-----step5:新备节点查看系统表dbe_perf.track_memory_context中DMSMemContext字段是否存在  ' \
               'expect:无相关字段及信息-----'
        self.log.info(text)

        res7 = self.pri_sh.execut_db_sql(sql1)
        self.log.info(res7)
        self.assertIn('t', res7.splitlines()[-2].strip(), '内存上下文设置失败')

        res8 = self.pri_sh.execut_db_sql(sql2, dbname='postgres')
        self.log.info(res8)
        self.assertIn('(0 rows)', res8, '存在异常信息')

    def tearDown(self):
        text = '-----step6:恢复初始环境  expect:恢复成功-----'
        self.log.info(text)

        restore = self.pri_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(restore)
        self.assertIn(self.constant.cm_switchover_success_msg, restore, '执行失败')

        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn('Primary Normal', status.splitlines()[-1].split('|')[0],
                      '恢复集群初始状态失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
